package com.stu.dao.impl;

import com.stu.dao.StudentDao;
import com.stu.model.StudentAnswer;
import com.stu.model.User;
import com.stu.util.DaoUtile;

import java.sql.SQLException;
import java.util.List;

public class StudentDaoImpl implements StudentDao {
    //获取所有老师的信息
    @Override
    public List<User> getAllTeacher() throws SQLException {
        String sql="select * from user where userIdentify=1";
    /*    System.out.println(sql);*/
        return DaoUtile.getAllUser(sql);
    }
 //模糊查询老师
    @Override
    public List<User> getSearchTeacher(String searchText) throws SQLException {
        String sql="select * from user where userIdentify=1 and (userAccount='"+searchText+"'or userName like '%"+searchText+"%' or userOtherName like'%"+searchText+"%')";
        return DaoUtile.getAllUser(sql);
    }
    //查询我已选择的老师
    @Override
    public List<User> goMyTeacher(String userAccount) throws SQLException {
        String sql="select * from user where userAccount in(select teacherAccount as userAccount from userteacher ut where ut.studentAccount='" + userAccount+ "')";
    /*    System.out.println(sql);*/
        return DaoUtile.getAllUser(sql);
    }
     //查询该老湿是否已经被学生选择
    @Override
    public int isExistStuTea(String userAccount, String teacherAccount) throws SQLException {
        String sql="select count(*) num from userteacher where studentAccount='"+userAccount+"'and teacherAccount='"+teacherAccount+"'";
        return DaoUtile.isExist(sql);
    }
       //添加老师
    @Override
    public void addTeacher(String userAccount, String teacherAccount) throws SQLException {
        String sql="insert into userteacher values('"+userAccount+"','"+teacherAccount+"')";
        DaoUtile.notQuery(sql);
    }
   //查询指定学生账户和教师账户,获取学生的任务信息
    @Override
    public List<StudentAnswer> goMyTeaTask(String userAccount, String teacherAccount) throws SQLException {
        String sql = "select s.*,u.userName,taskName from studenttask s inner join user u on s.studentAccount=u.userAccount " +
                "inner join task t on s.taskAccount=t.taskAccount where s.studentAccount='" + userAccount + "'and t.teacherAccount='"+teacherAccount+"'";
        System.out.println(sql);
        return DaoUtile.getTaskDetail(sql);
    }
//删除已选择的老师
    @Override
    public void deleteMyTeacher(String userAccount, String teacherAccount) throws SQLException {
        String sql1="delete from userteacher where studentAccount ='"+userAccount+"'and teacherAccount='"+teacherAccount+"'";
        String sql2="delete from studenttask where taskAccount in(select taskAccount from task where teacherAccount='"+teacherAccount+"')and studentAccount='"+userAccount+"'";
        DaoUtile.notQuery(sql1);
        DaoUtile.notQuery(sql2);
    }
      //添加学生的答案
    @Override
    public void doAnswer(String userAccount, String taskAccount, String answer) throws SQLException {
        String sql="update studenttask set studentAnswer ='"+answer+"',isFinish=1 where taskAccount='"+taskAccount+"'and studentAccount='"+userAccount+"'";
        DaoUtile.notQuery(sql);
    }
   //将批改分数添加到学生任务表中
    @Override
    public void dealFenshu(String taskAccount, String studentAccount, String fenshu) throws SQLException {
        String sql="update studenttask set score ='"+fenshu+"',isFinish=2 where taskAccount='"+taskAccount+"'and studentAccount='"+studentAccount+"'";
        DaoUtile.notQuery(sql);

    }


}
